﻿
using BCGM.MedicalWisdom.IServices;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.Models.Patients;
using BCGM.MedicalWisdom.Model.ViewModels.PatientsDTO;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq.Expressions;

namespace BCGM.MedicalWisdom.Api.Controllers
{
    /// <summary>
    /// 后台患者管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class patientController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IpatientServices _patientServices;
        private readonly IDepartmentServices _departmentServices;

        public patientController(IpatientServices patientServices, IDepartmentServices departmentServices)
        {
            _patientServices = patientServices;
            _departmentServices = departmentServices;
        }
        /// <summary>
        /// 患者注册
        /// </summary>
        /// <param name="patientLoginDTO"></param>
        /// <returns></returns>
        //[HttpPost]
        //[Route("Token")]
        //public async Task<MessageModel<string>> PatientLogin(PatientLoginDTO patientLoginDTO)
        //{

        //    string jwtStr = string.Empty;
        //    bool suc = false;
        //    //这里就是用户登陆以后，通过数据库去调取数据，分配权限的操作

        //    var user = await _patientServices.PatientLogin(patientLoginDTO.UserId, patientLoginDTO.UserPwd);
        //    if (user != null)
        //    {

        //        TokenModelJwt tokenModel = new TokenModelJwt { Uid = 1, Role = user.Pname };

        //        jwtStr = JwtHelper.IssueJwt(tokenModel);
        //        suc = true;
        //    }
        //    else
        //    {
        //        jwtStr = "login fail!!!";
        //    }

        //    return new MessageModel<string>()
        //    {
        //        success = suc,
        //        msg = suc ? "获取成功" : "获取失败",
        //        response = jwtStr
        //    };
        //}


        
        /// <summary>
        /// 患者列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<patient>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<patient, bool>> whereExpression = a => true;

            return new MessageModel<PageModel<patient>>()
            {
                msg = "获取成功",
                success = true,
                response = await _patientServices.QueryPage(whereExpression, page, intPageSize)
            };

        }
        /// <summary>
        /// 患者列表（收货地址+诊断信息）
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<PatientInfoDTO>>> GetPatient(int page = 1, int intPageSize = 50)
        {
            SugarParameter[] parameters = null;
            Expression<Func<PatientInfoDTO, bool>> whereExpression = a => true;
            return new MessageModel<PageModel<PatientInfoDTO>>()
            {
                msg = "获取成功",
                success = true,
                response = await _patientServices.GetPatient(whereExpression, page, intPageSize, parameters)
            };

        }
        /// <summary>
        /// 患者详情列表（收货地址+诊断信息）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<PatientDiagnoseInfoDTO>>> GetPatientInfo(int id,int page = 1, int intPageSize = 50)
        {
            //SugarParameter[] parameters = null;
            return new MessageModel<PageModel<PatientDiagnoseInfoDTO>>()
            {
                msg = "获取成功",
                success = true,
                response = await _patientServices.GetPatientInfo( page, intPageSize,id)
            };

        }
        /// <summary>
        /// 根据Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<patient>> GetById(string id)
        {
            return new MessageModel<patient>()
            {
                msg = "获取成功",
                success = true,
                response = await _patientServices.QueryById(id)
            };
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] patient request)
        {
            var data = new MessageModel<string>();

            var id = await _patientServices.Add(request);
            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] patient request)
        {
            var data = new MessageModel<string>();
            data.success = await _patientServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.PatientId.ObjToString();
            }

            return data;
        }
        /// <summary>
        /// 删除（逻辑删）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            var model = await _patientServices.QueryById(id);
            model.IsDeleted = true;
            data.success = await _departmentServices.Update(model);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = model?.PatientId.ObjToString();
            }

            return data;
        }
    }
}